<!DOCTYPE html>
<?php
session_start();
ob_start();
?>
<html>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <link rel="stylesheet" type="text/css" href='../css/style.css'>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <title>Demons</title>

    <body>
        <div class="body">
            <div id="menu">
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="#">New</a></li>
                    <li><a href="#">Contact</a></li>
                    <li><a href="#">About</a></li>
                </ul>
            </div>
            <div id="noidung">
                <h1 align="center">NEW WORKS</h1>
                <br>
                <div class="ajax_show"></div>
                <br>
                <div>
                    <?php
                    if (isset($_GET["logout"]) && $_GET["logout"] == true) {
                        unset($_SESSION["id"]);
                        header("location:login.php");
                    }
                    ?>
                    <?php if (isset($_SESSION['id'])): ?>
                        <a href="index.php?logout=true"><h2>Logout</h2></a>
                    <?php else: ?>
                        <a href="login.php"><h2>Login</h2></a>
                    <?php endif; ?>
                    <br>
                </div> 
                <br>
                <br>
                <?php
                require_once '../model/db.php';
                require_once '../model/keywords.php';
                require_once '../model/works.php';
                require_once '../model/pagination.php';
                $works = new works();
                $pag = new pagination();
                ?>

                <?php $page = $works->count() / PER_PAGE; ?>

                <?php if (!isset($_GET['page'])): ?>
                    <a href='index.php?page=<?php echo $_GET['page'] = 1; ?> '></a>
                <?php endif; ?>

                <a href='index.php?page=1'>FIRST </a>
                <a href='index.php?page=<?php echo $_GET["page"] + 1 ?>'> NEXT -> </a>

                <?php for ($i = 1; $i <= 10; $i++): ?>
                    <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                        <?php for ($i = 1; $i <= 10; $i++): ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->page = PER_PAGE; ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                            <?php endif; ?>
                        <?php endfor; ?>


                    <?php elseif (($_GET["page"] == $page) || ($_GET["page"] == $page - 1) || ($_GET["page"] == $page - 2) || ($_GET["page"] == $page - 3) || ($_GET["page"] == $page - 4)): ?>        
                        <?php for ($i = $page - 10; $i <= $page; $i++): ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->page = PER_PAGE; ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                            <?php endif; ?>
                        <?php endfor; ?>


                    <?php else : ?>
                        <?php for ($i = $_GET["page"] - 4; $i <= $_GET["page"] + 5; $i++): ?>
                            <a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a>
                            <?php if ($_GET["page"] == $i): ?>
                                <?php $works->page = PER_PAGE; ?>
                                <?php $works->start = (($i - 1) * $works->page); ?>
                            <?php endif; ?>
                        <?php endfor; ?>
                    <?php endif; ?> 
                <?php endfor; ?>   

                <a href='index.php?page=<?php echo $_GET["page"] - 1 ?>'> < - PREV</a>                
                <a href='index.php?page=<?php echo $page; ?>'> LAST </a>            

                <?php $list_of_works = $works->select(); ?>

                <br>
                <div class="">
                    <table border="1px">
                        <tr>
                            <td>id </td>
                            <td>name </td>
                            <td>Link </td> 
                            <td>DK </td>                    
                            <td>images</td>
                            <td>Function</td>
                        </tr>
                        <?php foreach ($list_of_works as $list): ?>
                            <tr>
                                <td><?php echo $list["id"]; ?></td>
                                <td><?php echo $list["name"]; ?></td>
                                <td><a href='<?php echo $list["url"]; ?>'><?php echo $list["url"]; ?></td>
                                <td><?php echo substr($list["description"], 0, 70); ?></td>
                                <td><img src="<?php echo $list['image']; ?>" width="120px"></td>
                                <?php if (isset($_SESSION['id']) && $_SESSION['id'] == 0) : ?>
                                    <td>
                                        <a href="upload.php?id=<?php echo $list['id']; ?>"><?php echo "EDIT" ?></a><br>
                                        <a href="#" class="ijax_view" id_works="<?php echo $list['name']; ?>"><?php echo "VIEW" ?></a><br>
                                        <a href='#'><?php echo "DELETE" ?></a><br>
                                    </td>
                                <?php else: ?>
                                    <td>

                                        <a href="#" class="ijax_view" id_works="<?php echo $list['name']; ?>"><?php echo "VIEW" ?></a><br>

                                    </td>
                                <?php endif; ?>
                            <?php endforeach; ?></tr>
                    </table>
                </div>

                <br>

            </div>
        </div>
        <br>       
        <script type="text/javascript" >
            $(document).ready(function() {
                $(".ijax_view").click(function() {
                    id_works = $(this).attr('id_works');
                    url = "ijax.php?id_works=" + id_works;
                    $.ajax({
                        url: url
                    }).done(function(data) {
                        $('.ajax_show').html(data);
                    });
                });
            });
        </script>
    </body>
</html>
<?php ob_end_flush(); ?>